package com.personal.controller;


import com.personal.controller.result.BackCode;
import com.personal.controller.result.Result;
import com.personal.domain.Enterprise;
import com.personal.service.EnterpriseService;
import com.personal.util.partDisplay.EnterprisePart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;

//    获取数据表内所有数据，只做开发调试，不参与应用
    @GetMapping("/All")
    public Result selectAll(){

        List<Enterprise> list=enterpriseService.selectAll();
        return new Result(list!=null? BackCode.GET_OK:BackCode.GET_ERR,list);
    }
    @GetMapping("")
    public Result selectPart(){
        List<EnterprisePart> list=enterpriseService.selectPartAll();
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @GetMapping("/search")
    public Result selectPartByKey(String customerName,String customerCode){
       List<EnterprisePart> list=enterpriseService.selectPartByKey(customerName,customerCode);
       System.out.println(list==null);
       return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }


    @PostMapping()
    public Result add(@RequestBody Enterprise enterprise){
        boolean flag=enterpriseService.add(enterprise);
        return new Result(flag?BackCode.UPDATE_OK:BackCode.UPDATE_ERR,flag);
    }


    @GetMapping("/updateData")
    public Result getUpdateData(@RequestParam("id") int id){
        Enterprise enterprise=enterpriseService.getUpdateData(id);
        return new Result(enterprise!=null?BackCode.GET_OK:BackCode.GET_ERR,enterprise);
    }
    @PutMapping()
    public Result update(@RequestBody Enterprise enterprise){
        boolean flag=enterpriseService.update(enterprise);
        return new Result(flag?BackCode.UPDATE_OK:BackCode.UPDATE_ERR,flag);
    }

    @DeleteMapping()
    public Result delete(@RequestParam List<Integer> ids){
        boolean flag=enterpriseService.delete(ids);
        return new Result(flag?BackCode.GET_OK:BackCode.GET_ERR,flag);

    }
}
